//
//  WZHAction.swift
//  WZHVerCodeInput
//
//  Created by 汪志浩 on 2022/11/15.
//

import Foundation
import UIKit

typealias WZHActionHandle = ()->()
public class WZHAction: UIView {
    
    @IBOutlet weak var titleLab: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    
    public var action : WZHActionHandle?
    
    public var select : WZHActionHandle?
    public convenience init(imageName:String? = nil,title:String? = nil,action : @escaping WZHActionHandle){
        self.init(frame: .zero)
        
        self.action = action
        
        if let imageName = imageName , let img = UIImage(named: imageName) {
            self.imageView.isHidden = false
            self.imageView.image = img
        }else{
            self.imageView.isHidden = true
        }
        
        if let title = title {
            self.titleLab.isHidden = false
            self.titleLab.text = title
        }else{
            self.titleLab.isHidden = true
        }
      
       
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    public func configUI() {
        self.backgroundColor = UIColor.white
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = .gray
        line.translatesAutoresizingMaskIntoConstraints = false
        line.heightAnchor.constraint(equalToConstant: 0.5).isActive = true
        line.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        line.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        line.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        
        
        let view = Bundle.main.loadNibNamed("WZHAction", owner: self)?.first as! UIView
        self.addSubview(view)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        view.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tap))
        self.addGestureRecognizer(tap)
        
        self.layer.cornerRadius = 25
        self.layer.masksToBounds = true
    }
    
    public @objc func tap(){
        self.action?()
        self.select?()
    }
}
